<template>
  <div class="discount-code">
    <popup  :isShow.sync="showCouponPop"
            :title="'输入'+title"
            @confirm="confirm">
      <template slot="content">
        <div class="discount-code-con">
          <div class="coupon-code-box">
            <input type="text" class="input_coupon_code" v-model="code" :placeholder="'请输入'+title">
            <span v-if="is_wx==1" class="wechatQR" @click="handleClickScan"></span>
          </div>
          <p class="erro_info" v-html="info"></p>
        </div>
      </template>
    </popup>
  </div>
</template>

<script>
import Vue from 'vue'
import popup from '@/components/popup'
import { getCouponCodeResult } from '@/api/order/order.js'
export default Vue.extend({
  components: {
    popup
  },
  props: {
    title: {
      type: String,
      default: '优惠码'
    },
    is_wx: {
      type: Number,
      default: 0
    },
    itemIds: {
      type: Array,
      default() {
        return []
      }
    },
    payment: {
      type: String,
      default: ''
    },
    current_price: {
      type: Array,
      default() {
        return []
      }
    },
    num: {
      type: Array,
      default() {
        return []
      }
    }
  },
  data() {
    return {
      showCouponPop: false,
      code: '',
      info: '',
      ret: false
    }
  },
  methods: {
    /**
     * 微信扫码
     */
    handleClickScan() {
        wx.scanCode({
          success: (res) => {
            // console.log(res)
            this.scanResutl(res)
          }
        })
    },
    /**
     * 获取地址栏?上的值
     */
    getParam(paramName, url) {
      var reg = new RegExp('(^|&)' + paramName + '=([^&]*)(&|$)', 'i')
      var r = url.split('?')[1].substr(1).match(reg)
      if (r != null) {
        return unescape(r[2])
      }
      return ''
    },
    /**
     * 处理扫码的结果
     */
    scanResutl(res) {
      const resultStr = res.result
      if (resultStr.includes('couponCode')) {
        this.code = this.getParam('coupon_code', resultStr)
      } else {
        this.code = resultStr.split('/')[6]
      }
    },
    // 优惠码核对
    getCouponCodeResultFun() {
      const data = {
        'coupon_code': this.code,
        'item_ids': this.itemIds,
        'payment': this.payment,
        'current_price': this.current_price,
        'num': this.num
      }
      if (this.code.length > 0) {
        getCouponCodeResult(data).then(res => {
          if (res.status == 1) {
            this.$parent.couponCodePrice = parseFloat(res.coupon_price)
            this.$parent.formData.coupon_code = this.code
            this.$parent.formData.coupon_code_price = res.coupon_price
            this.$parent.formData.coupon_code_name = res.coupon_code_name
          } else {
            this.info = res.msg
            this.ret = true
          }
        })
      } else {
        this.info = this.title + '有误，请重新输入'
        this.ret = true
      }
    },
    confirm(callback) {
      this.getCouponCodeResultFun()
      if (this.ret) {
        callback(false)
      }
    }
  }
})
</script>

<style lang="scss">
.discount-code-con{
  padding: 70px 0 140px 0;
  .coupon-code-box{
    display: flex;
    justify-content: center;
    align-items: center;
    .input_coupon_code{
      width:600px;
      padding: 20px 25px;
      height:80px;
      background:rgba(247,247,247,1);
      border-radius:40px;
    }
    span{
      display: block;
      width: 54px;
      height: 54px;
      background: url(https://img.wifenxiao.com/h5-wfx/images/order/wx_code.png) center /cover no-repeat;
      margin-left: 35px;
    }
  }
  .erro_info{
    font-size: 24px;
    text-align: center;
    color: #F30C23;
    padding-top: 30px;
  }
}
</style>